<template>
  <div class="mod-config">
    <basic-container>
      <div class="boxsty zhiBiao" v-if="zhiShow==1">
        <div style="height: 50px; border-bottom: 1px solid #b0bde2">
          <el-button type="primary" @click="ndts()">拟定推送</el-button>
        </div>
      <div class="avue-crud">
        <el-table
          ref="table"
          style="width: 100%;margin-top:20px"
          :data="dataList"
          border
          v-loading="dataListLoading">
          <el-table-column type="selection"  width="55" header-align="center" align="center"></el-table-column>
          <el-table-column type="index" width="70" header-align="center" align="center" label="序号"></el-table-column>
          <el-table-column
            prop="contractName"
            header-align="center"
            align="center"
            width="160"
            label="合同名称">
            <template slot-scope="scope">
                <span  v-if="permissions.hetongrenwu_hetonghtxxdjb_edit" @click="addOrUpdateHandle(scope.row)" style="color:#0e258c;cursor: pointer;">{{scope.row.contractName}}</span>
                <span v-else>{{scope.row.contractName}}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="radio == 2"
            prop="jigouName"
            header-align="center"
            align="center"
            label="机构名称">
          </el-table-column>
          <el-table-column v-if="radio==1"
            prop="shouyueName"
            header-align="center"
            align="center"
            min-width="120"
            label="受约人姓名">
            <template slot-scope="scope">
              <div v-for="(item,index) in cutout(scope.row.shouyueName).slice(0, 2)">
                <el-tooltip class="item" :disabled="cutout(scope.row.shouyueName).length<3" effect="dark" :content="scope.row.shouyueName" placement="top-start">
                  <span>{{item}}<span v-if="cutout(scope.row.shouyueName).length>2&&index==1">({{cutout(scope.row.shouyueName).length}}人)</span></span>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="shouyueZhiwu"
            header-align="center"
            align="center"
            min-width="180"
            label="受约人职务">

            <template slot-scope="scope">
              <div v-for="item in cutout(scope.row.shouyueZhiwu).slice(0, 2)">
                <el-tooltip class="item"  :disabled="item.length<8&&cutout(scope.row.shouyueZhiwu).length<3" effect="dark" :content="scope.row.shouyueZhiwu" placement="top-start">
                  <span>{{validatenull(item)?'-':item | ellipsis}}</span>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="fayueName"
            header-align="center"
            align="center"
            min-width="120"
            label="发约人姓名">
            <template slot-scope="scope">
              <div v-for="(item,index) in cutout(scope.row.fayueName).slice(0, 2)">
                <el-tooltip :disabled="cutout(scope.row.fayueName).length<3" class="item" effect="dark" :content="scope.row.fayueName" placement="top-start">
                <span>{{item}}<span v-if="cutout(scope.row.fayueName).length>2&&index==1">({{cutout(scope.row.fayueName).length}}人)</span></span>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="fayueZhiwu"
            header-align="center"
            align="center"
            min-width="180"

            label="发约人职务">
            <template slot-scope="scope">
              <div v-for="item in cutout(scope.row.fayueZhiwu).slice(0, 2)">
                <el-tooltip class="item" :disabled="item.length<8&&cutout(scope.row.fayueZhiwu).length<3" effect="dark" :content="scope.row.fayueZhiwu" placement="top-start">
                    <span>{{(validatenull(item)?'-':item) | ellipsis}}</span>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="weight"
            header-align="center"
            align="center"
            min-width="180"
            label="权重">
          </el-table-column>
          <el-table-column
            prop="quotaauditStatus"
            header-align="center"
            align="center"
            min-width="120"
            label="指标审核状态">
            <template slot-scope="scope">
              <el-popover
                placement="left"
                width="120"
                trigger="hover">
                <p>
                <div v-for="item in cutout(scope.row.quotaauditStatusstr)">
                  <span v-if="item.indexOf('已完成')!=-1" style="color:#0bbf4a">{{item}}</span>
                  <span v-else-if="item.indexOf('未完成')!=-1" style="color:red">{{item}}</span>
                  <span v-else>{{item}}</span>
                </div>
                <div slot="reference" class="name-wrapper">
                  <span v-if="scope.row.quotaauditStatus == 0">未填报</span>
                  <span v-else-if="scope.row.quotaauditStatus == 1">审核中</span>
                  <span v-else>已审核</span>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            prop="contractAuditStatus"
            header-align="center"
            align="center"
            width="120"
            label="合同状态">
            <template slot-scope="scope">
              <span v-html="cutout2(scope.row.contractAuditStatus)"/>
            </template>
          </el-table-column>
        </el-table>
      </div>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <HeTongForm v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" @openZhibiao="openZhibiao" @refreash="refreashdata"></HeTongForm>
      <!-- 编辑-->
      <zhibiaoForm ref="zhibiaoRef" v-if="zhiShow==2" :zhibiaoForm='zhibiaoForm'  v-on:zhibiaoChild='zhibiaoChild' :hetongId="hetongId"></zhibiaoForm>
    </basic-container>
    <!--    发送指标部门-->
    <el-dialog title="请选择指标拟定部门" :close-on-click-modal="false" :visible.sync="zbvisible" append-to-body>
      <div style="height: 50vh">
        <el-row>
          <el-col :span="8">
            <div class="text-center">
              <el-tree
                :data="treeOrgData"
                show-checkbox
                node-key="id"
                :default-expanded-keys="[2, 3]"
                :default-checked-keys="[5]"
                :props="defaultProps"
                @check-change="handleCheckChange">
              </el-tree>
            </div>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ts()">推送</el-button>
      <el-button type="primary" plain @click="zbvisible = false">返回#</el-button>
    </span>

    </el-dialog>
  </div>

</template>

<script>
import {fetchList, delObj,radioTwoList} from '@/api/hetongrenwu/hetonghtxxdjb'
import { getNiDingList,Htongndts,tuisongwenben,faqiqianding,deleteBatchHtcj,getSumWeight } from '@/api/hetongrenwu/hetongyeji/hetongnd'
import TableForm from '@/views/hetongrenwu/hetong/hetongyeji/hetonghtxxdjb/hetonghtxxdjb-form'
import HeTongForm from '@/views/hetongrenwu/components/hetong-form'
import zhibiaoForm from '@/views/hetongrenwu/components/zhibiao-form.vue'
import { page } from '@/api/hetongrenwu/hetongyeji/hetongnd'
import {mapGetters} from 'vuex'
import { validatenull } from "@/util/validate";

export default {
  components: {
    TableForm,
    HeTongForm,
    zhibiaoForm
  },
  data() {
    return {
      streamId: 0,
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      radio:'1',
      radioTwo:'-1',
      zhiShow:'1',
      zbvisible:false,
      hetongId:Number,
      zhibiaoForm:{
      },
      treeOrgData:[],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      confirmTreeData:[
        {
          id: 5,
          label: '二级机构'
        }
      ],
      selectedTreeData:[],
      searchadvanced: true,
      searchheight: '0',
      radioTwoList:radioTwoList
    }
  },
  watch:{
    radio(value){
      if(value){
        this.getDataList();
      }
    },
    radioTwo(value){
      if(value){
        this.getDataList();
      }
    }
  },
  filters:{
        ellipsis(value){
          if (!value) return '';
          if (value.length > 8) {
            return value.slice(0,8) + '...'
          }
          return value
        }
      },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
      init(formInfo,
            formInfoDetailed,
            formData,
            GUID,
            wfYwMain,
            wfYwStream,
            dqYwStream,
            dqTask){
                this.streamId = dqYwStream.id
                this.dataForm.id = wfYwMain.ywLyid
                this.getDataList()
                this.getTreeData()
        },
        getFormData() {
            this.$emit("returnFormData", null);
        },
    searchchangeadvanced() {
      this.searchadvanced = !this.searchadvanced;
      if(this.searchadvanced){
        this.searchheight= '50'
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      page(Object.assign({
        id:this.dataForm.id,
      })).then(response => {
        this.dataList = response.data.data.records;
        this.totalPage = response.data.data.total;
        this.$nextTick(function(){
          this.dataListLoading = false
        })
      })

    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle(obj) {
      if(obj!=undefined){
        if(obj.contractAuditStatus==0){
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(obj)
          })
        }else{
          this.zhiShow=2
          this.hetongId=obj.id
          this.zhibiaoForm={
            enter:0,
            disabled:true

          }
          this.$nextTick(() => {
            this.$refs.zhibiaoRef.getDataList()
            this.$refs.zhibiaoRef.saveForm(obj)
          })
        }
      }else{
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(obj)
        })
      }


    },
    // 删除
    deleteHandle(id) {
      this.$confirm('是否确认删除ID为' + id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getDataList()
      })
    },
    //子集信息
    zhibiaoChild(zhibiaoChild){
      if(zhibiaoChild==1){
        this.zhiShow=1;
        this.getDataList();
      }
      if(zhibiaoChild=='关闭'){
        this.zhiShow=1;
        this.getDataList();
      }
    },
    //确定选择选中得机构
    fnConfirmSelectedData(){
      this.confirmTreeData=this.selectedTreeData
    },
    //树结构选中的数据
    handleCheckChange(data){
      var index =this.selectedTreeData.findIndex(item =>item.deptId==data.deptId)
      if(index==-1){
        this.selectedTreeData.push(data)
      }else{
        this.selectedTreeData.splice(index,1)
      }
    },
    //新增子集返回的数据(关闭修改页面，打开指标库维护)
    openZhibiao(data){
      this.addOrUpdateVisible = false;
      this.zhiShow=2;
      this.zhibiaoForm={
        enter:0,
        disabled:false
      }
    },
    //获取树结构
    getTreeData(){
      getNiDingList().then(res=>{
       //console.log("数："+JSON.stringify(res))
        this.treeOrgData=res.data.data
      })
    },
    tswb(){
      var data=this.$refs.table.selection;
      if(data.length==0){
        this.$message.error("请选择一个合同！");
        return;
      }
      if(data.length>1){
        this.$message.error("只能选择一个合同！");
        return;
      }
      if(data[0].quotaauditStatus!=2){
        this.$message.error("指标审核未完成，不允许推送文本审核！");
        return;
      }
      if(data[0].contractAuditStatus!=1 && data[0].contractAuditStatus!=3){
        this.$message.error("只允许推送创建和驳回的合同！");
        return;
      }
      getSumWeight({htId:data[0].id}).then(res => {
        if(res.data.data!=100){
          this.$confirm('当前合同的权重值为'+res.data.data+'%,是否要继续推送文本审核？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            return tuisongwenben(data[0].id)
          }).then(data => {
            this.$message.success('推送文本审核成功')
            this.getDataList()
          })
        }
      })
    },
    fqqd(){
      var data=this.$refs.table.selection;
      if(data.length==0){
        this.$message.error("请选择一个合同！");
        return;
      }
      if(data.length>1){
        this.$message.error("只能选择一个合同！");
        return;
      }
      if(data[0].contractAuditStatus!=4){
        this.$message.error("文本审核未完成，不允许发起签订！");
        return;
      }
      this.$confirm('是否要发起签订吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return faqiqianding(data[0].id)
      }).then(data => {
        this.$message.success('发起签订成功')
        this.getDataList()
      })
    },
    //拟定推送-外面
    ndts(){
      var data=this.$refs.table.selection;
      if(data.length==0){
        this.$message.error("请选择一个合同！");
        return;
      }
      if(data.length>1){
        this.$message.error("只能选择一个合同！");
        return;
      }
      if(data[0].contractAuditStatus!=0){
        this.$message.error("请选择创建中的合同！");
        return;
      }
      if(data[0].quotaauditStatusstr=='未填报'){
        this.zbvisible=true;
      }else{
        this.$confirm('是否要拟定推送？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return Htongndts({id:data[0].id})
        }).then(data => {
          this.$message.success('拟定推送成功')
          this.getDataList()
        })
      }
    },
    fszbbumen(){
      var data=this.$refs.table.selection;
      if(data.length==0){
        this.$message.error("请选择一个合同！");
        return;
      }
      if(data[0].contractAuditStatus!=0){
        this.$message.error("请选择创建中的合同！");
        return;
      }
      this.zbvisible=true;
    },
    //拟定推送-里面
    ts(){
      var data=this.$refs.table.selection;
      if(this.selectedTreeData.length==0){
        this.$message.error("拟定部门不能为空！");
        return false;
      }
      var ids=this.selectedTreeData.map(item=>{
        return item.deptId;
      })
      var idsStr=ids.join(',')
      var streamId = this.streamId
      this.$confirm('是否要拟定推送？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return Htongndts({id:data[0].id,ndDeptId:idsStr,streamId:streamId})
      }).then(data => {
        this.$message.success('拟定推送成功')
        this.zbvisible=false;
        this.getDataList()
      })
    },
    deleteBatch(){
      var data=this.$refs.table.selection;
      if(data.length==0){
        this.$message.error("请至少选择一个合同！");
        return;
      }
      var ids=[];
      for(var i=0;i<data.length;i++){
        ids.push(data[i].id)
      }
      var idsStr=ids.join(',')
      this.$confirm('是否要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return deleteBatchHtcj({ids:idsStr})
      }).then(data => {
        this.$message.success('删除成功')
        this.getDataList()
      })
    },
    zbtb(){
      var data=this.$refs.table.selection;
      if(data.length==0){
        this.$message.error("请选择一个合同！");
        return;
      }
      if(data.length>1){
        this.$message.error("只能选择一个合同！");
        return;
      }

      this.zhiShow=2
      this.hetongId=data[0].id
      this.zhibiaoForm={
        enter:0,
        disabled:true

      }
      this.$nextTick(() => {
        this.$refs.zhibiaoRef.getDataList()
        this.$refs.zhibiaoRef.saveForm(data[0])
      })
    },
    refreashdata(data){
      if(data==1){
        this.getDataList();
      }
      if(data=='closeDialog'){
        this.getDataList();
        this.addOrUpdateVisible=false
      }
    },
    cutout(cellValue) {
      if(cellValue){
        return cellValue.split(',')
      }
    },
    cutout2(cellValue) {
      if(cellValue==0){
        return '创建中';
      }
      if(cellValue==1){
        return '已拟定推送';
      }
      if(cellValue==2){
        return '文本审核中';
      }
      if(cellValue==3){
        return '文本审核退回';
      }
      if(cellValue==4){
        return '文本审核完成';
      }
      if(cellValue==5){
        return '合同签订中';
      }
      if(cellValue==6){
        return '合同签订已退回';
      }
      if(cellValue==7){
        return '签订审核通过';
      }
    },
    //判空
    validatenull(val){
     return validatenull(val)
    }
  }
}
</script>

